<?php

class CategoryController extends Controller {

    public $layout = "column2";

    /**
     * 首页
     * @throws CHttpException
     */
    public function actionIndex() {
        $alias = $_GET['master'];
        $model = SubsiteCategory::model()->findByAttributes(array('title_alias' => $alias));
        if ($model == null)
            throw new CHttpException(404, 'The Category does not exist.');


        if (in_array($model->product_model, array('PRODUCT', 'ARCHIVE'))) {
            $sub_category = SubsiteCategory::model()->getSubCategorys($model->id);
            $this->render('index', array(
                'model' => $model,
                'sub_category' => $sub_category,
            ));
        } else {
            $model = SubsiteCategory::model()->findByAttributes(array('title_alias' => $alias));
            //游戏区
            $game_servers = ProductCategory::model()->getGameServers($model->game_id);

            $this->render('product', array(
                'master' => $model,
                'model' => $model,
                'game_servers' => $game_servers
            ));
        }
    }

    /**
     * 产品页
     */
    public function actionProduct() {
        $master_alias = Yii::app()->request->getQuery('master'); //上级栏目
        $alias = Yii::app()->request->getQuery('alias'); //本栏目
        $masterCategory = SubsiteCategory::model()->findByAttributes(array('title_alias' => $master_alias));
        $model = SubsiteCategory::model()->findByAttributes(array('title_alias' => $alias));
        //游戏区
        $game_servers = ProductCategory::model()->getGameServers($model->game_id);

        $this->render('product', array(
            'master' => $masterCategory,
            'model' => $model,
            'game_servers' => $game_servers
        ));
    }

    /**
     * API
     */
    public function actionApi() {
        $action = $_GET['action'];
        switch ($action) {
            case 'PLP'://代练计算价格和时间
                $game_id = (int) $_GET['game_id'];
                $s_level = (int) $_GET['s_level'];
                $end_level = (int) $_GET['e_level'];
                $plData = ProductPowerleveling::model()->getPowerlevelPH($game_id, $s_level, $end_level);
                $price = $this->changeCurrency($plData->pl_price);
                $jArray['price'] = $price['tag'] . $price['price'];
                $jArray['hour'] = round($plData->pl_time, 2);
                echo json_encode($jArray);
                break;
            case 'SKILLP'://技能代练
                $game_id = (int) $_GET['game_id'];
                $skill_id = (int)$_GET['skill_id'];
                $s_level = (int) $_GET['s_level'];
                $end_level = (int) $_GET['e_level'];
                $params = array(
                    'game_id' => $game_id,
                    'skill_id' => $skill_id,
                    'start_level' => $s_level,
                    'end_level' => $end_level,
                    'attack' => '',
                    'defence' => 1,
                    'strength' => 1
                );
                $data = ProductSkill::model()->getSkillLevelPH($params);
                $price = $this->changeCurrency($data->skill_price);
                $jArray['price'] = $price['tag'] . $price['price'];
                $jArray['hour'] = round($data->skill_time, 2);
                echo json_encode($jArray);
                break;
            default:
                echo json_encode(array('code' => -1, 'msg' => 'Action Error!'));
                break;
        }
    }

}